package com.yesjpt.web;

import com.alibaba.fastjson2.JSONObject;
import com.yesjpt.domain.Lxfs;
import com.yesjpt.domain.Person;
import com.yesjpt.domain.User;
import com.yesjpt.util.L;
import com.yesjpt.util.StringUtil;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

@RestController
@RequestMapping("/hello") // 上下文路径
public class HelloController {
    @Resource
    private JdbcTemplate jdbcTemplate;

    @GetMapping("/testJdbc")
    public Object testJdbc() {
        L.begin(new Throwable().getStackTrace()[0]);
        return jdbcTemplate.queryForList("select * from hello.t_notice where id = ?", "2"); //建议加上模式/数据库的名称，有些数据库下面有模式
    }

    @GetMapping("/testClass")
    public Object testClass() {
        L.begin(new Throwable().getStackTrace()[0]);
        Person vo = new Person();
        Lxfs l = new Lxfs();
        l.setAddr("二环路");
        l.setPhone("13709088388");
        l.setBirth(new Date());
        vo.setBirthday(new Date());
        vo.setId("001");
        vo.setNickname("老陈");
        vo.setName("陈剑波");
        vo.setLxfs(l);
        System.out.println("vo = " + vo);
        return vo;
    }

    @PostMapping("/add")
    public Object add(@RequestBody User vo) {
        L.begin(new Throwable().getStackTrace()[0] + ", vo = " + vo);
        vo.setCreateTime(new Date());
        vo.setId(StringUtil.genGuid());
        return vo;
    }

    @PutMapping("/update")
    public Object u(User vo) {
        L.begin(new Throwable().getStackTrace()[0] + ", vo = " + vo);
        vo.setUpdateTime(new Date());
        return vo;
    }

    //读取环境变量
    @GetMapping("/get")
    public Object get(User vo) {
        L.begin(new Throwable().getStackTrace()[0] + ", vo = " + vo);
        vo.setUpdateTime(new Date());
        //vo.setName("朱旻昊");
        return vo;
    }

    @GetMapping("/findById")
    public Object findById(String id) {
        L.begin(new Throwable().getStackTrace()[0] + ", id = " + id);
        JSONObject o = new JSONObject();
        o.put("success", true);
        o.put("user.id", id);
        return o;
    }

    @DeleteMapping("/delete")
    public Object delete(User vo) {
        L.begin(new Throwable().getStackTrace()[0] + ", vo = " + vo);
        vo.setName(vo.getName() + "已遭删除");
        return vo;
    }

    @GetMapping("/getLxfs")
    public Object getLxfs() {
        L.begin(new Throwable().getStackTrace()[0]);
        Lxfs lxfs = new Lxfs();
        lxfs.setAddr("金牛区");
        lxfs.setEmail("293440@qq.com");
        lxfs.setPhone("13709088388");
        lxfs.setBirth(new Date());
        return lxfs;
    }

}
